package com.felix.tech.ocms.repository;

import com.felix.tech.ocms.domain.User;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * {@link User} {@link Repository}
 */
@Repository
public class UserRepository {

    /**
     * 采用内存型的存储方式 -> Map
     */
    private final ConcurrentMap<Integer, User> repository = new ConcurrentHashMap<Integer, User>();

    private final static AtomicInteger idGenerator = new AtomicInteger();
    /**
     * 保存用户信息.
     * @param user
     * @return
     */
    public Boolean save(User user){
        final Integer id = idGenerator.incrementAndGet();
        user.setId(id);
        return repository.put(id, user) == null;
    }

    /**
     * 返回所有用户列表
     * @return Collection<User>
     */
    public Collection<User> findAll(){
        return repository.values();
    }
}
